package com.baizhi.springbootday0202.controller;

import com.baizhi.springbootday0202.entity.Auction;
import com.baizhi.springbootday0202.service.AuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/auction")
public class AuctionController {
	@Autowired
	private AuctionService auctionService;

	@RequestMapping("/showAuction")
	@ResponseBody
	public Map showAuction(int page,int limit){
		System.out.println(page+"------"+limit);
		return auctionService.selectAuction(page,limit);
	}


	@RequestMapping("/uploadFile")
	@ResponseBody
	public Map uploadFile(MultipartHttpServletRequest request, MultipartFile auctionImg, Auction auction) throws IOException {
		System.out.println("更新中");
//		文件对象的接收方式和以前还是一样的
//		先进行文件的上传
		System.out.println(auctionImg);
		System.out.println(auction);

//		获取项目中的文件存放文件夹的目录
		String upload = request.getServletContext().getRealPath("upload");
//		新的文件名
		String newFileName = UUID.randomUUID().toString().replace("-","")+auctionImg.getOriginalFilename();
//		将文件存放在项目的文件夹中

		auctionImg.transferTo(new File(upload+"/"+newFileName));

//		将文件路径以及接收到的数据进行修改
		auction.setAuctionPic(newFileName);
		Map map = auctionService.updateAuction(auction);
		return map;
	}



	@RequestMapping("/deleteAuction")
	@ResponseBody
	public Map deleteAuction(int id){
		Map map = auctionService.deleteAuction(id);
		return map;
	}


	@RequestMapping("/insertAuction")
	@ResponseBody
	public Map insertAuction(MultipartHttpServletRequest request,MultipartFile auctionImg,Auction auction) throws IOException {
		System.out.println("添加中");
//		获取上文件的路径
		String realPath = request.getServletContext().getRealPath("upload");

//		将文件赋予一个新的文件名
		String newFileName = UUID.randomUUID().toString().replace("-","")+auctionImg.getOriginalFilename();

//		将文件上传到项目指定的文件下
		auctionImg.transferTo(new File(realPath+"/"+newFileName));

		auction.setAuctionPic(newFileName);
		Map map = auctionService.insertAuction(auction);
		return map;
	}


	//根据页面的传参   查询一个auction的对象



}
